package com.glory.dmegcpda.adapter

import android.widget.ImageView
import android.widget.RadioButton
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.glory.dmegcpda.R
import com.glory.dmegcpda.bean.CheckInfoEntity
import android.text.Editable

import android.text.TextWatcher

import android.widget.TextView

import android.widget.EditText
import java.lang.NumberFormatException


/**
 * FileName: CheckInfoAdapter
 * Author: XJH
 * Date: 2021/10/21 16:33
 * Description:
 * History:
 */
class CheckInfoAdapter(data: List<CheckInfoEntity.DataInfoEntity>) :
    BaseQuickAdapter<CheckInfoEntity.DataInfoEntity, BaseViewHolder>(
        R.layout.item_check_info_list,
        data as MutableList<CheckInfoEntity.DataInfoEntity>
    ) {
    override fun convert(helper: BaseViewHolder, item: CheckInfoEntity.DataInfoEntity) {
        helper.setText(R.id.tvCheckTitle,item.NAME).setText(R.id.tvPosition,(helper.adapterPosition + 1).toString())
        helper.setGone(R.id.etCheckValue,true).setGone(R.id.rbGroupLayout,false)
        val ivInstructions :ImageView = helper.getView(R.id.ivInstructions)
        val rbOK :RadioButton = helper.getView(R.id.rbOK)
        val rbNg :RadioButton = helper.getView(R.id.rbNG)
        when(item.CHECKVALUE){
            "OK" ->{
                rbOK.isChecked = true
                ivInstructions.setImageDrawable(context.resources.getDrawable(R.drawable.shape_green_fill_circle))
            }
            "NG"->{
                rbNg.isChecked = true
                ivInstructions.setImageDrawable(context.resources.getDrawable(R.drawable.shape_red_fill_circle))
            }
        }
//        if (item.CHECKVALUE == "OK") rbOK.isChecked = true
//        if (item.CHECKVALUE == "NG") rbNg.isChecked = true

        rbOK.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked){
                item.CHECKVALUE = "OK"
                ivInstructions.setImageDrawable(context.resources.getDrawable(R.drawable.shape_green_fill_circle))
            }else{
                ivInstructions.setImageDrawable(context.resources.getDrawable(R.drawable.shape_red_fill_circle))
            }
        }

        rbNg.setOnCheckedChangeListener { _, isChecked ->
            if (!isChecked){
                item.CHECKVALUE = "NG"
                ivInstructions.setImageDrawable(context.resources.getDrawable(R.drawable.shape_green_fill_circle))
            }else{
                ivInstructions.setImageDrawable(context.resources.getDrawable(R.drawable.shape_red_fill_circle))
            }
        }
        setEditListener(helper.getView(R.id.etComment),item)

    }

    private fun setEditListener(editText: EditText, item: CheckInfoEntity.DataInfoEntity) {
        if (editText.tag is TextWatcher) {
            editText.removeTextChangedListener(editText.tag as TextWatcher)
        }
        val watcher: TextWatcher = object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable) {
               item.COMMENTS = s.toString()
            }
        }
        editText.addTextChangedListener(watcher)
        editText.tag = watcher
    }
}